package com.hxd.mall.widget.refresh;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.hxd.mall.R;


public class LoadingView extends View {
    private Bitmap bitmap;
    private Paint paint1;
    private Paint paint2;
    private Paint paint3;
    private Canvas canvas;
    private int h, w;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint1 = new Paint();
        paint1.setColor(getResources().getColor(R.color.color_1153FC));
        paint2 = new Paint();
        paint2.setColor(Color.parseColor("#CCCCCC"));
        paint3 = new Paint();
        paint3.setColor(Color.WHITE);
        Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.refresh_no_hint);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap2, 238, 42, true);
            bitmap = bitmap1.extractAlpha();// 获取一个透明图片
            h = bitmap.getHeight();//初始化y轴坐标
            w = bitmap.getWidth();//初始化x轴坐标
        } else {
            bitmap = bitmap2.extractAlpha();// 获取一个透明图片
            h = bitmap.getHeight();//初始化y轴坐标
            w = bitmap.getWidth();//初始化x轴坐标
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            float textHeight = bitmap.getHeight();
            height = (int) (getPaddingTop() + textHeight + getPaddingBottom());
        }

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            float textWidth = bitmap.getWidth();
            width = (int) (getPaddingLeft() + textWidth + getPaddingRight());
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        canvas.drawColor(Color.TRANSPARENT); // 画布颜色
        canvas.drawBitmap(bitmap, 0, 0, paint2); //画一个灰的图片
        canvas.save();
        //按X来裁剪区域
        canvas.clipRect(0, 0, w, bitmap.getHeight());
        canvas.drawBitmap(bitmap, 0, 0, paint1);
        canvas.restore();
    }

    public void updateView(int i) {
        h = i;
        w = i;
        postInvalidate();
    }
}
